import BaseUI from "../script/utils/BaseUI";
import BigNumber from '../script/utils/BigNumber';

const wx = window["wx"];

const { ccclass, property } = cc._decorator;

@ccclass
export default class RankLayer extends BaseUI {
    @property(cc.Prefab)
    item_pre: cc.Prefab = null;
    start() {
        this.updateList([]);
        wx.onMessage((res) => {
            if (res.text == "hello") {
                let arr = []
                Promise.all([this.getFriendCloudStorage(), this.getPotentialFriendList()]).then(values => {
                    if (values[0]) {
                        let tmparr = values[0] as [];
                        for (var tmp of tmparr) {
                            this.kvlist2kvobj(tmp)
                        }
                        arr = arr.concat(tmparr.sort((a: any, b: any) => {
                            return b.kvobj["lv"] - a.kvobj["lv"]
                        }))
                    }

                    if (values[1])
                        arr = arr.concat(values[1])

                    this.updateList(arr);
                })
            }
            else if (res.text == "bye") {
                this.updateList([]);
            }
        })
    }

    getPotentialFriendList() {
        return new Promise((resolve, rejcet) => {
            wx.getPotentialFriendList({
                success: (res) => {
                    console.log("getPotentialFriendList");
                    resolve(res.list);
                },
                fail: (res) => {
                    resolve([]);
                }
            })
        })
    }

    getFriendCloudStorage() {
        return new Promise((resovle, rejcet) => {
            wx.getFriendCloudStorage({
                keyList: ["lv"],
                success: (res) => {
                    console.log("getFriendCloudStorage", res);
                    resovle(res.data)
                },
                fail: () => {
                    resovle([])
                }
            })
        })
    }


    kvlist2kvobj(info) {
        let kvobj = {}
        for (var tmp of info.KVDataList) {
            kvobj[tmp.key] = tmp.value;
        }
        info.kvobj = kvobj;
    }

    updateList(list: any[]) {
        this.GetGameObject("content").removeAllChildren();
        for (var i = 0; i < list.length; ++i) {
            let info = list[i];
            if (info) {
                let node = cc.instantiate(this.item_pre);
                node.parent = this.GetGameObject("content")
                node.getChildByName("button_gold").active = !info.KVDataList;
                if (info.kvobj) {
                    console.log(info)
                    let lv = Number(info.kvobj["lv"]) || 0;

                    node.getChildByName("head").getChildByName("lbl_zichan").getComponent(cc.Label).string = BigNumber.getLargeString(lv);
                    node.getChildByName("lbl_rank").getComponent(cc.Label).string = (i + 1) + "";
                    node.getChildByName("new").active = false;
                }
                else {
                    node.getChildByName("lbl_rank").getComponent(cc.Label).string = "";
                    node.getChildByName("new").active = true;
                }

                node.getChildByName("head").getChildByName("lbl_name").getComponent(cc.Label).string = info.nickname;
                node.getChildByName("button_gold")["info"] = info;
                if (info.avatarUrl && info.avatarUrl != "") {
                    let head = node.getChildByName("head");
                    cc.loader.load({ url: info.avatarUrl, type: 'jpg' }, (err, tex) => {
                        if (!err) {
                            if (!cc.isValid(head)) return;
                            head.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(tex);
                        }
                    });
                }
            }
        }
    }
}
